var amqp = require('amqplib/callback_api');

amqp.connect('amqp://Arrcen:arrcen911@59.110.172.150:5672/', function (err, conn) {
    if (err) { exit(err); }
    conn.createChannel(function (err, ch) {
        if (err) { exit(err); }

        // var ex = 'lg.RefundMessage';
        var ex = 'Arrcen.EventMessageExchange.ChanJoy.Messaging.Model.HisMessage.RefundMessage';

        ch.assertExchange(ex, 'fanout', {durable: false});
        
        ch.assertQueue('', {exclusive:true, autoDelete:true, durable: false}, function (err, q) {
            ch.bindQueue(q.queue, ex, '');
            
            console.log(" [*] Waiting for refund messages in %s. To exit press CTRL+C", q);
            ch.consume(q.queue, function (msg) {
                let now = new Date();
                console.log(" %s [*] Received %s", now.toLocaleString(),msg.content.toString());
            }, {noAck: true});
        });
    })
});

function exit(err) {
    console.log(err);
    process.exit(1);
}